package com.tfjybj.itoo.exam.provider.service.impl;

import com.dmsdbj.itoo.tool.base.service.impl.BaseServicePlusImpl;
import com.dmsdbj.itoo.tool.constantutil.YearSemester;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.TeacherCourseEntity;
import com.tfjybj.itoo.exam.model.StaffModel;
import com.tfjybj.itoo.exam.model.TeacherCourseModel;
import com.tfjybj.itoo.exam.provider.dao.TeacherCourseDao;
import com.tfjybj.itoo.exam.provider.service.TeacherCourseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * TeacherCourseService接口实现类
 * ${base}表
 *
 * @author 杨江涛
 * @version 2.0.0
 * @since 2.0.0 2018-11-16 20:35:34
 */
@Service("teacherCourseService")
public class TeacherCourseServiceImpl extends BaseServicePlusImpl<TeacherCourseDao, TeacherCourseEntity> implements TeacherCourseService {

    //region 模板生成

    @Resource
    private TeacherCourseDao teacherCourseDao;

    @Override
    public PageInfo<TeacherCourseEntity> queryByLikeCourseId(String courseId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(teacherCourseDao.queryLikeCourseId(courseId));
    }

    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * @param teacherId 教师_id
     * @author 杨江涛
     * @since 2018年11月17日16:01:12
     */
    @Override
    public List<TeacherCourseEntity> findByTeacherId(String teacherId) {

        //从工具类中获取学年学期数据
        String semester = YearSemester.getYearSemesterCode(0, 0).get(0);
        TeacherCourseEntity teacherCourse = new TeacherCourseEntity();
        teacherCourse.setSchoolYear(semester);
        teacherCourse.setTeacherId(teacherId);
        return teacherCourseDao.selTeacherTable(teacherCourse);
    }

    @Override
    public String queryCourseName(String courseId) {
        return teacherCourseDao.queryCourseName(courseId);
    }

    @Override
    public List<TeacherCourseEntity> findTeacherByTeacherId(String teacherId) {
        return teacherCourseDao.findTeacherByTeacherId(teacherId);
    }

    @Override
    public List<StaffModel> findTeacherByCourseId(String courseId) {
        return teacherCourseDao.findTeacherByCourseId(courseId);
    }


    /**
     * 根据查询参数模糊查找该教师及课程信息
     *
     * @param searchInfo 查询参数
     * @author 杨江涛
     * @since 2.0.0 2018-12-7 10:39:04
     */
    @Override
    public List<TeacherCourseModel> findTeacherCourse(String searchInfo) {
        return teacherCourseDao.findTeacherCourse(searchInfo);
    }

    /**
     * 根据登录人id查询课程
     *
     * @param teacherId teacherId
     * @return 登陆人所有的课程
     * @author maying
     * @since 2018-12-12 08:55:58
     */
    @Override
    public List<TeacherCourseModel> loginCourse(String teacherId) {

        return teacherCourseDao.loginCourse(teacherId, YearSemester.getYearSemesterCode(0, 0).get(0));
    }

    /**
     * 根据学生id查询该学生,该学年课程
     *
     * @param studentId
     * @author 杨海云
     * @since 2.0.0 2019-2-24
     */
    @Override
    public List<TeacherCourseEntity> findByStudentId(String studentId) {
        String schoolYear = YearSemester.getYearSemesterCode(0, 0).get(0);
        List<TeacherCourseEntity> teacherCourseEntityList = teacherCourseDao.findByStudentIdAndSchoolYear(studentId, schoolYear);
        return teacherCourseEntityList;
    }

    @Override
//    TODO 待删除
    public TeacherCourseEntity queryCourseTeacher(String Operator) {
        return teacherCourseDao.queryCourseTeacher(Operator);
    }


    /**
     * 查询该教师教过的所有课程
     *
     * @param teacherId 教师_id
     * @author 杜雨
     * @since 2019年6月20日
     */
    @Override
    public List<TeacherCourseEntity> findAllByTeacherId(String teacherId) {
        TeacherCourseEntity teacherCourse = new TeacherCourseEntity();
        teacherCourse.setTeacherId(teacherId);
        List<TeacherCourseEntity> teacherCourseList = teacherCourseDao.selTeacherTable(teacherCourse);

        // 根据课程id 过滤重复记录
        teacherCourseList = teacherCourseList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(TeacherCourseEntity::getCourseId))), ArrayList::new));

        return teacherCourseList;
    }

}
